<template>
  <div class="update-gender">
    <van-picker
      title="修改性别"
      show-toolbar
      :columns="columns"
      :default-index="defaultIndex"
      @confirm="onConfirm"
      @cancel="$emit('close')"
      @change="onGenderChange"
    />
  </div>
</template>

<script>
import { updateUserProfile } from "@/api/user";
export default {
  // 组件名称
  name: "UpdateGender",
  // 组件参数 接收来自父组件的数据
  props: {
    value: {
      type: Number,
      required: true,
    },
  },
  // 局部注册的组件
  components: {},
  // 组件状态值
  data() {
    return {
      columns: ["男", "女"],
      defaultIndex: this.value,
    };
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  created() {},
  mounted() {},
  // 组件方法
  methods: {
    onGenderChange(picker, value, index) {
      this.defaultIndex = index;
    },
    async onConfirm() {
      this.$toast.loading({
        message: "保存中...",
        forbidClick: true,
      });
      await updateUserProfile({
        gender: this.defaultIndex,
      });
      this.$emit("input", this.defaultIndex);
      this.$emit("close");
      this.$toast.success("保存成功");
    },
  },
};
</script> 

<style scoped lang='less'>
</style>
